package com.atguigu.eduorder.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.eduorder.entity.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117605715";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key ="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC12o+/tAWXgPrLtvAWFFEXWuTbYMPdCtV9dl3/kVgGT0xpQNmNCT3T5f4FZXYu734SfBTYssaExohbSeibKRJtBpYYsqCnwmbjIF7OpU2GEkuJeiT0B0bpQePjYtQ549I3QUQH97w2J1crBsohv33yzgu8FMHD8z4RSpD+df5apGYieJKBF2MDjgg9dB3rpVGzPSAFHPmxCm2r/KaBrSA+7h7viF7PRTGKSyEWWNtIHMai8D7Z6Kr4bm5UHErBffO2aeZHcqf1HodoryFRf1Ae7k3KNyEY93ZKjTjEeGgueKQH7Bf3rDc0ipDWA41Fio2665po1jW0gR2Lv3AUW6xrAgMBAAECggEAF1sPtT5IrZGaJEnFggBv/cDlvasq3JvEmY13OxiOppjOpoH2rt9t+mYCfuzuBc8qcPNQthgX2PuqjZbrceoPfMMoydk/OuyDzE3e/0lJsJtu6iQu1MyOdezJeO27dgflesU+GIvft1DMKDGCCl2Orn16EljUHfeClDPwdU3wMfHNw3VDnNi2sg9JMmIR3ala84s25P5oTaksO3BWBfLGDmLXUW2VjELt4qdjtWa1m8lWj9LAUnif67Usa3otWmfsTG9J7RpWRv5I8GAQOQ55p7XFxAljn9lCJqr6WvBc5kdMRQw4+Dnz7pBZTWp7MZ9oVr14XvKkJFZCc46KzB866QKBgQDm48uyCHKmgXD/X2Ui+biq9aBsyF59qXCiQKoLM2i/7MT2qq/3TkD28ePIEDjhSvYWX8U2E5ijWYmLDfaWA8L1G+1ASl9Q9PZ9r/z5K2hkfjjdEVN2vunhw29z6TZ1PaMIFTdj302STJjiL7TFc33U5LY9DthB6wjjiUwGuubs/QKBgQDJoY1tC1qRqTJgvtCIC/PfEledfAcbCQvShITl/tSKn3KOi4/1Fru3RxEM5wdHj8BVKmasRuhGBXrT/G85LdxeTrTIZ/HPtM1fvpDdcmDtkZRlQv9aYmuu3n5desp8NoIife3aIVgpOXJe4RNNJ7WdnHT1Uh0/CJFbA9mf1Q9vhwKBgQCzp3gttSkQqhN0gZO+XTT1wV3shxQPDzKW7V3ijUPqYp73l+SHt5oRDDJCz2yS0LMewbI/24ufPuIpEtpV6+NcuTgoGDaGVbUeIzvh3YbC+vvf98VL1k1B38HvHP3EyAr9FRUeXviK55LmHFy6bpzwdG3goJZg5DLsaT6A1hktOQKBgBrJtuNs5byCcSn/T+8zP1Q8uJ04tCz4V7TBXHK+F3jPVEXx7s9OFOR1saM4MnRkedYWywtKZGOhKL9vQ0uidyukY0pY3PyOddapl8+D+bK2a7PEYe6dMtdJwhojTTGRgFqaxnX7hh37/uDyIEFY7D0jYzqQjUgpmD4yVcBxaxqTAoGBAJUtum9Qk+l4zz+3k9R+ibQHVtlRkNoj/GAIVNtUTUA/z/3iWoDj8u5lFSEdrLl2/cinO7tS6KI+KswMX2NxmYC8t1UCGcu/w2Nbn11cWIgAEC6nDvdhSSezwaS9MsrmcUj4p09HkUyZyWamPmV7DgJ0xDirgYCnDDid5ERKwXYw";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnvqA1X9+xyhXkxd/s8YzY7YIPmb/IMA0eFMPipCu6npTC+VLH1ufXqrln4ZsiU8GtumXZkar4FoDy/1wDAFXrH7WK+0/PwQvD0JV8fu0kHVPJJToSO9zt4OYcBUvPEfvW8xYFa6lQ6tzxkMbxgbdJBb2RtRhsICE9m4BMlqarJ7YpVN1Bpwt/uk83q/7MMLll5bdicCo3izpWQED4Xy3VNo7InioE7vd7h41AzVPyck9zxfwYAXzO3nRHu26pPAo15jyUEgv+Lq+NAxuRT32fvT2WJGcmNIBoopbixqiP4+AJEuKn8t1eggsS0Ht8enCDWxyulbsaOaJDc7bVwUN9wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页--项目的订单详细页面
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";



    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

       return_url="http://localhost:8007/eduorder/order/payok";
        notify_url="http://localhost:8007/eduorder/order/payok/";

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = String.valueOf(vo.getPrice());
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
